package biz.bbtec.sbm.issuetracking.action;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import biz.bbtec.sbm.issuetracking.bean.AttachmentBean;
import biz.bbtec.sbm.issuetracking.bean.TaskBean;
import biz.bbtec.sbm.issuetracking.factory.TaskFactory;
import biz.bbtec.sbm.issuetracking.service.TaskService;
import biz.bbtec.sbm.user.bean.UserBean;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class TaskAction extends ActionSupport implements SessionAware {

	private TaskBean task = new TaskBean();
	private List<TaskBean> tasklist = new ArrayList();
	private List<TaskBean> tasklist2 = new ArrayList<TaskBean>();
	private List<TaskBean> tasks;
	private int pageNow;
	private List<UserBean> involvelist = new ArrayList<UserBean>();
	private List<UserBean> userlist = new ArrayList<UserBean>();

	public List<UserBean> getInvolvelist() {
		return involvelist;
	}

	public void setInvolvelist(List<UserBean> involvelist) {
		this.involvelist = involvelist;
	}
	private String taskname;
	public String getTaskname() {
		return taskname;
	}

	public void setTaskname(String taskname) {
		this.taskname = taskname;
	}

	public List<UserBean> getUserlist() {
		return userlist;
	}

	public void setUserlist(List<UserBean> userlist) {
		this.userlist = userlist;
	}

	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}

	public List<TaskBean> getTasklist2() {
		return tasklist2;
	}

	public void setTasklist2(List<TaskBean> tasklist2) {
		this.tasklist2 = tasklist2;
	}

	public List<TaskBean> getTasks() {
		return tasks;
	}

	public void setTasks(List<TaskBean> tasks) {
		this.tasks = tasks;
	}

	private Map session;

	public Map getSession() {
		return session;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public List<TaskBean> getTasklist() {
		return tasklist;
	}

	public void setTasklist(List<TaskBean> tasklist) {
		this.tasklist = tasklist;
	}

	public TaskBean getTask() {
		return task;
	}

	public void setTask(TaskBean task) {
		this.task = task;
	}

	public String addTask() throws Exception {
		TaskService taskservice = TaskFactory.getInstance().creatTaskService();
		if (!taskservice.AddTask(task)) {
			this.addActionError("数据库操作失败！");
			return INPUT;
		} else {
			return SUCCESS;
		}
	}

	public String modifyTask() throws Exception {
		TaskService taskservice = TaskFactory.getInstance().creatTaskService();
		if (!taskservice.Modify(task)) {
			this.addActionError("数据库操作失败！");
			return INPUT;
		}
		Map session = ActionContext.getContext().getSession();
		session.remove("task");
		this.addActionError("添加成功！");
		return SUCCESS;
	}

	public String deleteTask() throws Exception {
		TaskService taskservice = TaskFactory.getInstance().creatTaskService();
		List<AttachmentBean> attlist = taskservice.attlist(task);
		if (attlist.size() != 0) {
			String path = ServletActionContext.getServletContext().getRealPath(
					"images");
			for (int i = 0; i < attlist.size(); i++) {
				File f = new File(path + "\\" + attlist.get(i).getTitle());
				if (f.exists()) {
					f.delete();
				}
			}
		}
		if (!taskservice.DeleteTask(task)) {
			this.addActionError("未知错误！");
			return INPUT;
		} else {
			this.addActionError("删除成功！");
			return SUCCESS;
		}
	}

	public String showallTask() throws Exception {
		TaskService taskservice = TaskFactory.getInstance().creatTaskService();
		tasklist = taskservice.ShowAllTask(getPageNow(), 15);
		if (getPageNow() == 0) {
			int total = taskservice.CountAllTask();
			int page = 0;
			do {
				total = total - 15;
				page++;
			} while (total > 0);
			this.session.put("tasktotalpage", page);
		}
		if (tasklist.size() == 0) {
			this.addActionError("没有项目，请添加！");
			return INPUT;
		} else {
			return SUCCESS;
		}
	}

	public String showallName() throws Exception {
		TaskService taskservice = TaskFactory.getInstance().creatTaskService();
		tasklist = taskservice.ShowAllName();
		tasklist2 = taskservice.ShowAllName2();
		boolean flag = false;
		boolean flag2 = false;
		if (tasklist.size() != 0) {
			flag = true;
		}
		if (tasklist2.size() != 0) {
			flag2 = true;
		}
		if ((!flag) && flag2) {
			this.addActionError("没有项目供您选择！");
			return INPUT;
		} else if (flag && (!flag2)) {
			this.addActionError("您没有项目可以退选！");
			return INPUT;
		} else if ((!flag) && (!flag2)) {
			this.addActionError("数据库中无任何公共项目，请添加！");
			return INPUT;
		}
		return SUCCESS;
	}

	public String showoneTask() throws Exception {
		TaskService taskservice = TaskFactory.getInstance().creatTaskService();
		task = taskservice.ShowOneTask(task);
		if (task == null) {
			this.addActionError("出错啦！");
			return INPUT;
		} else {
			this.session.put("task", task);
			return SUCCESS;
		}
	}

	public String chooseTask() throws Exception {
		TaskService taskservice = TaskFactory.getInstance().creatTaskService();
		if (!taskservice.ChooseTask(tasks)) {
			this.addActionError("未知错误！");
			return INPUT;
		}
		return SUCCESS;
	}

	public String showallinvolve() throws Exception {
		TaskService taskservice = TaskFactory.getInstance().creatTaskService();
		involvelist = taskservice.ShowALLInolve(task.getTaskid());
		if (involvelist.size() == 0) {
			this.addActionError("无相关工作人员!");
			return INPUT;
		}
		userlist = taskservice.ShowALLUser(task.getTaskid());
		if (userlist.size() == 0) {
			this.addActionError("无相关工作人员!");
			return INPUT;
		}
		String name = taskservice.showTaskName(task.getTaskid());
		task.setName(name);
		return SUCCESS;
	}

}
